/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2017 KYE Company                                       */
/* 跨越速运集团有限公司版权所有                                           */
/*                                                                        */
/* PROPRIETARY RIGHTS of KYE Company are involved in the                */
/* subject matter of this material. All manufacturing, reproduction, use, */
/* and sales rights pertaining to this subject matter are governed by the */
/* license agreement. The recipient of this software implicitly accepts   */
/* the terms of the license.                                              */
/* 本软件文档资料是跨越速运集团有限公司的资产，任何人士阅读和                   */
/* 使用本资料必须获得相应的书面授权，承担保密责任和接受相应的法律约束。                 */
/*                                                                        */
/**************************************************************************/

/**
  * <pre>
  * 作   者：liwei
  * 创建日期：2018-1-4
  * </pre>
  */

package com.kyexpress.common.hrinterview.provider.service;

import java.util.ArrayList;
import java.util.List;

import com.kyexpress.common.hrinterview.api.bo.EmployeeStudyExpBO;
import com.kyexpress.common.hrinterview.provider.utils.ErrorEnum;
import com.kyexpress.common.idcenter.IdGenClient;
import com.kyexpress.framework.exception.ApplicationException;
import com.kyexpress.framework.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kyexpress.framework.service.GenericService;
import com.kyexpress.common.hrinterview.api.model.EmployeeStudyExp;
import com.kyexpress.common.hrinterview.provider.mapper.EmployeeStudyExpMapper;
import org.springframework.util.CollectionUtils;

/**
 * <pre>
 * 面试人员的学习经历业务类
 * </pre>
 */
@Service("employeeStudyExpService")
public class EmployeeStudyExpService extends GenericService<EmployeeStudyExp, Long> {
    public EmployeeStudyExpService(@Autowired EmployeeStudyExpMapper employeeStudyExpMapper) {
        super(employeeStudyExpMapper);
    }
    
    public EmployeeStudyExpMapper getMapper() {
        return (EmployeeStudyExpMapper) super.genericMapper;
    }

    /**
     * 教育经历批量操作
     * @author liwei
     * @date 2018-01-04
     * @param bo
     * @return
     */
    public void batchOperation(EmployeeStudyExpBO bo) {
        if (null == bo) {
            return;
        }
        if (!CollectionUtils.isEmpty(bo.getAddList())) {
            List<EmployeeStudyExp> addDatas = new ArrayList<>();
            for (EmployeeStudyExp tmp : bo.getAddList()) {
                EmployeeStudyExp data = new EmployeeStudyExp();
                data.setId(IdGenClient.getId());
                data.setEmployeeid(tmp.getEmployeeid());
                data.setStartTime(null == tmp.getStartTime() ? null : tmp.getStartTime());
                data.setEndTime(null == tmp.getEndTime() ? null : tmp.getEndTime());
                data.setSchool(formatString(tmp.getSchool()));
                data.setEducation(tmp.getEducation());
                data.setProfession(formatString(tmp.getProfession()));
                addDatas.add(data);
            }
            this.insertBatch(addDatas);
        }
        if (!CollectionUtils.isEmpty(bo.getUpdateList())) {
            List<EmployeeStudyExp> updateDatas = new ArrayList<>();
            for (EmployeeStudyExp tmp : bo.getUpdateList()) {
                EmployeeStudyExp data = new EmployeeStudyExp();
                checkIdLegal(tmp.getId());
                data.setId(tmp.getId());
                data.setEmployeeid(tmp.getEmployeeid());
                data.setStartTime(null == tmp.getStartTime() ? null : tmp.getStartTime());
                data.setEndTime(null == tmp.getEndTime() ? null : tmp.getEndTime());
                data.setSchool(formatString(tmp.getSchool()));
                data.setEducation(tmp.getEducation());
                data.setProfession(formatString(tmp.getProfession()));
                updateDatas.add(data);
            }
            this.updateSelective(updateDatas.toArray(new EmployeeStudyExp[updateDatas.size()]));
        }
        if (!CollectionUtils.isEmpty(bo.getDeleteList())) {
            List<Long> batchOperationIdList = new ArrayList<>();
            for (EmployeeStudyExp tmp : bo.getDeleteList()) {
                checkIdLegal(tmp.getId());
                if (!batchOperationIdList.contains(tmp.getId())) {
                    batchOperationIdList.add(tmp.getId());
                }
            }
            Long [] batchOperationIdArray = batchOperationIdList.toArray(new Long[batchOperationIdList.size()]);
            this.disable(batchOperationIdArray);
        }
    }

    private String formatString(String str) {
        return StringUtils.isRealEmpty(str) ? null : str.trim();
    }

    private void checkIdLegal(Long id) {
        if (null == id) {
            throw new ApplicationException(ErrorEnum.IdNull.getCode(), ErrorEnum.IdNull.getMsg());
        }
    }
}